package org.example.ui.administrator;

import org.example.business.MilkManageBusiness;
import org.example.business.MilkTeaBusiness;
import org.example.manage.MilkTeaManage;
import org.example.model.MilkTea;

import javax.swing.*;
import javax.swing.table.TableCellRenderer;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

public class MilkTeaAdminItem extends JPanel {
    public JTextField[] textFields = new JTextField[6];
    private JLabel imageLabel;
    private JButton[] buttons = new JButton[4];
    private SpringLayout layout = new SpringLayout();
    private JCheckBox box;
    public boolean isEdit = false;
    public static boolean isSave = true;
    private MilkTea milkTea, uMilkTea;
    public MilkTeaAdminItem(MilkTea milkTea, int n) {
        setLayout(layout);
        setBackground(Color.WHITE);
        uMilkTea = milkTea;
        this.milkTea = new MilkTea();
        Font font = new Font("", Font.BOLD, 15);
        setBorder(BorderFactory.createLineBorder(Color.BLACK));
        String[] milkStr = {String.valueOf(n), milkTea.milkName, milkTea.kind,
                String.valueOf(milkTea.inventory), String.valueOf(milkTea.price), milkTea.condition};
        JTable table = new JTable();
        TableCellRenderer renderer = table.getDefaultRenderer(Boolean.class);
        box = (JCheckBox) renderer;
        Image image2 = new ImageIcon("background/bag_icon.jpg").getImage().
                getScaledInstance(10, 10, Image.SCALE_DEFAULT);
        box.setIcon(new ImageIcon(image2));
        box.setSelectedIcon(new ImageIcon(image2));
        add(box);
        layout.putConstraint(SpringLayout.WEST, box, 5, SpringLayout.WEST, this);
        layout.putConstraint(SpringLayout.NORTH, box, 40, SpringLayout.NORTH, this);
        for (int i = 0; i < 6; i++) {
            textFields[i] = new JTextField();
            textFields[i].setText(milkStr[i]);
            textFields[i].setBorder(BorderFactory.createLineBorder(Color.LIGHT_GRAY));
            textFields[i].setFont(font);
            textFields[i].setHorizontalAlignment(JTextField.CENTER);
            textFields[i].setFocusable(false);
            textFields[i].setOpaque(false);
            add(textFields[i]);
            if (i == 2) {
                Image image = new ImageIcon(milkTea.imagePath).getImage().
                        getScaledInstance(75, 75, Image.SCALE_DEFAULT);
                imageLabel = new JLabel(new ImageIcon(image), JLabel.CENTER);
                imageLabel.setBorder(BorderFactory.createLineBorder(Color.LIGHT_GRAY));
                add(imageLabel);
                layout.putConstraint(SpringLayout.WEST, imageLabel, 0, SpringLayout.EAST, textFields[i-1]);
                layout.putConstraint(SpringLayout.NORTH, imageLabel, 0, SpringLayout.NORTH, this);
                layout.putConstraint(SpringLayout.SOUTH, imageLabel,0, SpringLayout.SOUTH, this);
                layout.putConstraint(SpringLayout.WEST, textFields[i], 0, SpringLayout.EAST, imageLabel);

            }
            if (i == 0) {
                layout.putConstraint(SpringLayout.WEST, textFields[i], 0, SpringLayout.EAST, box);
            }
            else {
                if (i !=2)  layout.putConstraint(SpringLayout.WEST, textFields[i], 0, SpringLayout.EAST, textFields[i-1]);
            }
            layout.putConstraint(SpringLayout.NORTH, textFields[i], 0, SpringLayout.NORTH, this);
            layout.putConstraint(SpringLayout.SOUTH, textFields[i],0, SpringLayout.SOUTH, this);
        }
        layout.putConstraint(SpringLayout.EAST, textFields[0], 65, SpringLayout.EAST, box);
        layout.putConstraint(SpringLayout.EAST, textFields[1], 120, SpringLayout.EAST, textFields[0]);
        layout.putConstraint(SpringLayout.EAST, imageLabel, 130, SpringLayout.EAST, textFields[1]);
        layout.putConstraint(SpringLayout.EAST, textFields[2], 130, SpringLayout.EAST, imageLabel);
        layout.putConstraint(SpringLayout.EAST, textFields[3], 100, SpringLayout.EAST, textFields[2]);
        layout.putConstraint(SpringLayout.EAST, textFields[4], 80, SpringLayout.EAST, textFields[3]);
        layout.putConstraint(SpringLayout.EAST, textFields[5], 80, SpringLayout.EAST, textFields[4]);
        String[] operate = {"修改", "删除", milkTea.condition.equals("上架")?"下架":"上架", "保存"};
        for (int i = 0; i < 4; i++) {
            buttons[i] = new JButton(operate[i]);
            add(buttons[i]);
            if (i == 0){
                layout.putConstraint(SpringLayout.WEST, buttons[i], 2, SpringLayout.EAST, textFields[5]);

            }
            else {
                layout.putConstraint(SpringLayout.WEST, buttons[i], 3, SpringLayout.EAST, buttons[i-1]);
            }
            layout.putConstraint(SpringLayout.NORTH, buttons[i], 35, SpringLayout.NORTH, this);
        }
        setAllListener();
        setVisible(true);
    }
    private void setAllListener() {
        buttons[0].addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                if (isSave) {
                    for (int i = 1; i < 5; i++) {
                        textFields[i].setFocusable(true);
                    }
                    isEdit = true;
                    isSave = false;
                    setBackground(Color.CYAN);
                }
                else {
                    JOptionPane.showMessageDialog(MilkTeaAdminItem.this, "你编辑的内容未保存，请保存");
                }
            }
        });
        buttons[3].addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                if (isEdit&&MilkTeaBusiness.newMilkTea(MilkTeaAdminItem.this,
                        textFields[4].getText(), textFields[3].getText(),
                        textFields[1].getText(), textFields[2].getText())) {
                    for (int i = 1; i < 5; i++) {
                        textFields[i].setFocusable(false);
                    }
                    milkTea.milkName = textFields[1].getText();
                    milkTea.kind = textFields[2].getText();
                    milkTea.price = Double.parseDouble(textFields[4].getText());
                    milkTea.inventory = Integer.parseInt(textFields[3].getText());
                    if (milkTea.imagePath.equals("background/bag_icon.jpg"))
                        milkTea.imagePath = uMilkTea.imagePath;
                    new MilkTeaManage().update(uMilkTea, milkTea);
                    isEdit = false;
                    isSave = true;
                    setBackground(Color.WHITE);
                    MilkManageBusiness.setAdminItems(0);
                    MilkTeaBusiness.setMilkTeas(0);
                    MilkTeaBusiness.setKinds();
                }
                else {
                    if (!isSave&& !isEdit)
                        JOptionPane.showMessageDialog(MilkTeaAdminItem.this, "你编辑的内容未保存，请保存");

                }
            }
        });
        buttons[1].addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                if (isSave) {
                    new MilkTeaManage().move(uMilkTea);
                    MilkManageBusiness.setAdminItems(0);
                    MilkTeaBusiness.setMilkTeas(0);
                    MilkTeaBusiness.setKinds();
                }
                else {
                    JOptionPane.showMessageDialog(MilkTeaAdminItem.this, "你编辑的内容未保存，请保存");
                }
            }
        });
        buttons[2].addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                if (isEdit) {
                    if (buttons[2].getText().equals("上架")) {
                        milkTea.condition = "上架";
                        buttons[2].setText("下架");
                        textFields[5].setText("上架");
                    }
                    else {
                        milkTea.condition = "下架";
                        textFields[5].setText("下架");
                        buttons[2].setText("上架");
                    }
                }
                else {
                    if (!isSave&& !isEdit)
                        JOptionPane.showMessageDialog(MilkTeaAdminItem.this, "你编辑的内容未保存，请保存");

                }
            }
        });
        imageLabel.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                if (isEdit) {
                    milkTea.imagePath = new MilkTea().savePhoto(uMilkTea.imagePath);
                    Image image = new ImageIcon(milkTea.imagePath).getImage()
                            .getScaledInstance(75, 75, Image.SCALE_DEFAULT);
                    imageLabel.setIcon(new ImageIcon(image));
                }
            }
        });
    }
}
